package org.serverct.finalfight.internal.info;

import me.clip.placeholderapi.PlaceholderAPI;
import net.minecraft.server.v1_16_R3.PacketPlayOutScoreboardDisplayObjective;
import net.minecraft.server.v1_16_R3.ScoreboardObjective;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_16_R3.scoreboard.CraftScoreboard;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.*;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.GameManager;
import org.serverct.finalfight.game.uhc.team.TeamPlayer;
import org.serverct.finalfight.game.uhc.team.TeamType;
import org.serverct.finalfight.game.uhc.team.UHCTeamGame;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.ResourceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class ScoreBoardHandler {
    public static String title;
    public static List<String> lines = new ArrayList<>();
    public static boolean enableNameTag;
    public static boolean enableTabList;

    public static void initialize() {
        ResourceUtils.autoUpdateConfigs("", "scoreboard.yml");
        YamlConfiguration yaml = CustomSettings.getYaml("", "scoreboard.yml");

        title = yaml.getString("title").replace("&", "§");
        lines = yaml.getStringList("lines");
        for (int i = 0; i < lines.size(); i++) {
            lines.set(i, lines.get(i).replace("&", "§"));
        }

        enableNameTag = yaml.getBoolean("enableNameTag");
        enableTabList = yaml.getBoolean("enableTabList");
    }

    public static void sendScoreBoard(BasicGame game) {
        Scoreboard share = Bukkit.getScoreboardManager().getNewScoreboard();

        Objective tab = share.registerNewObjective("Tab", "dummy", "§c血量", RenderType.HEARTS);
        tab.setDisplaySlot(DisplaySlot.PLAYER_LIST);
        Objective below = share.registerNewObjective("Below", "dummy", "§c血量", RenderType.HEARTS);
        below.setDisplaySlot(DisplaySlot.BELOW_NAME);

        boolean isTeamMode=game instanceof UHCTeamGame;
        HashMap<TeamType, Team> teams=new HashMap<>();

        new BukkitRunnable() {
            @Override
            public void run() {
                if (!GameManager.arenas.containsKey(game.id)) {
                    tab.unregister();
                    below.unregister();
                    this.cancel();
                    return;
                }
                if (game.state != GameState.REFRESHING) {
                    for (UUID uuid : game.players.keySet()) {
                        Player player = Bukkit.getPlayer(uuid);

                        if(isTeamMode&&game.state==GameState.RUNNING){
                            TeamPlayer teamPlayer= (TeamPlayer) game.players.get(uuid);
                            TeamType type=teamPlayer.team.type;
                            if(!teams.containsKey(type)){
                                Team team=share.registerNewTeam(type.name());
                                team.setDisplayName(type.getColoredName());
                                team.setColor(type.getChatColor());
                                team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.ALWAYS);
                                team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.FOR_OWN_TEAM);
                                team.setCanSeeFriendlyInvisibles(true);
                                team.setAllowFriendlyFire(false);
                                team.setPrefix(type.name()+" - ");
                                teams.put(type,team);
                            }
                            teams.get(type).addEntry(player.getName());
                        }

                        tab.getScore(player).setScore((int) player.getHealth());
                        below.getScore(player).setScore((int) player.getHealth());

                        //计分板
                        Scoreboard board=Bukkit.getScoreboardManager().getNewScoreboard();
                        Objective sidebar = board.registerNewObjective("FF-ScoreBoard", "dummy", PlaceholderAPI.setPlaceholders(player, title));
                        sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
                        int cnt = lines.size();
                        for (String string : lines) {
                            sidebar.getScore(PlaceholderAPI.setPlaceholders(player, string)).setScore(cnt--);
                        }

                        Objective tab_player = board.registerNewObjective("Tab", "dummy", "§c血量", RenderType.HEARTS);
                        tab_player.setDisplaySlot(DisplaySlot.PLAYER_LIST);
                        Objective below_player = board.registerNewObjective("Below", "dummy", "§c血量", RenderType.HEARTS);
                        below_player.setDisplaySlot(DisplaySlot.BELOW_NAME);

                        for (UUID other : game.players.keySet()) {
                            Player pl=Bukkit.getPlayer(other);
                            tab_player.getScore(pl).setScore((int) pl.getHealth());
                            below_player.getScore(pl).setScore((int) pl.getHealth());
                        }

                        player.setScoreboard(board);
                    }
                }else{
                    teams.clear();
                }
            }
        }.runTaskTimer(FinalFight.plugin, 0L, 10L);
    }
}
